package com.ghovos.usermangerpgdemo.common.result;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 全局返回
 *
 * @param <T> 动态数据类型
 */
@Data
@ApiModel(value = "全局统一返回结果")
public class GlobalResult<T> {
    @ApiModelProperty(value = "返回码")
    private int code;
    @ApiModelProperty(value = "返回消息")
    private String message;
    @ApiModelProperty(value = "数据")
    private T data;


    public GlobalResult() {
    }

    /**
     * 构建 返回值
     *
     * @param data 返回 数据
     * @param <T>  返回数据类型
     * @return 构建成果
     */
    protected static <T> GlobalResult<T> build(T data) {
        GlobalResult<T> result = new GlobalResult<>();
        if (data != null) {
            result.setData(data);
        }
        return result;
    }


    public static <T> GlobalResult<T> build(T data, GlobalResultEnum resultEnum) {
        GlobalResult<T> result = build(data);
        result.setCode(resultEnum.getCode());
        result.setMessage(resultEnum.getMessage());
        return result;
    }


    public static <T> GlobalResult<T> build(Integer code, String message) {
        GlobalResult<T> result = build(null);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    public static <T> GlobalResult<T> ok() {
        return ok(null);
    }

    public static <T> GlobalResult<T> ok(T data) {
        return GlobalResult.build(data, GlobalResultEnum.SUCCESS);
    }

    public static <T> GlobalResult<T> fail() {
        return fail((String) null);
    }

    public static <T> GlobalResult<T> fail(String message) {
        return GlobalResult.build(201, message);
    }

    public static <T> GlobalResult<T> fail(GlobalResultEnum resultEnum) {
        return GlobalResult.build(resultEnum.getCode(), resultEnum.getMessage());
    }


}
